<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="优惠券类型(1-满减,2-折扣,3-直扣)" prop="couponType">
        <el-select
          v-model="queryParams.couponType"
          placeholder="请选择优惠券类型(1-满减,2-折扣,3-直扣)"
          clearable
          style="width: 100px"
        >
          <el-option
            v-for="dict in coupon_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="满减门槛金额(类型1需要)" prop="couponFull">
        <el-input
          v-model="queryParams.couponFull"
          placeholder="请输入满减门槛金额(类型1需要)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="折扣率(0.9=9折)或直扣金额" prop="couponDeduction">
        <el-input
          v-model="queryParams.couponDeduction"
          placeholder="请输入折扣率(0.9=9折)或直扣金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大折扣金额(类型2需要)" prop="couponMaxdeduction">
        <el-input
          v-model="queryParams.couponMaxdeduction"
          placeholder="请输入最大折扣金额(类型2需要)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总库存" prop="couponTotal">
        <el-input
          v-model="queryParams.couponTotal"
          placeholder="请输入总库存"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="剩余库存" prop="couponRemain">
        <el-input
          v-model="queryParams.couponRemain"
          placeholder="请输入剩余库存"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['coupon:couponmanage:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['coupon:couponmanage:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['coupon:couponmanage:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['coupon:couponmanage:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="couponmanageList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="优惠券ID" align="center" prop="couponId" />
      <el-table-column
        label="优惠券类型(1-满减,2-折扣,3-直扣)"
        align="center"
        prop="couponType"
      >
        <template #default="scope">
          <dict-tag :options="coupon_type" :value="scope.row.couponType" />
        </template>
      </el-table-column>
      <el-table-column
        label="满减门槛金额(类型1需要)"
        align="center"
        prop="couponFull"
      />
      <el-table-column
        label="折扣率(0.9=9折)或直扣金额"
        align="center"
        prop="couponDeduction"
      />
      <el-table-column
        label="最大折扣金额(类型2需要)"
        align="center"
        prop="couponMaxdeduction"
      />
      <el-table-column label="总库存" align="center" prop="couponTotal" />
      <el-table-column label="剩余库存" align="center" prop="couponRemain" />
      <el-table-column
        label="优惠券创建时间"
        align="center"
        prop="couponCreatetime"
      />
      <el-table-column
        label="优惠券截止时间"
        align="center"
        prop="couponEndtime"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['coupon:couponmanage:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['coupon:couponmanage:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="couponmanageRef" :model="form" :rules="rules">
        <el-form-item
          label="优惠券类型(1-满减,2-折扣,3-直扣)"
          prop="couponType"
        >
          <el-select
            v-model="form.couponType"
            placeholder="请选择优惠券类型(1-满减,2-折扣,3-直扣)"
            style="width: 300px"
          >
            <el-option
              v-for="dict in coupon_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="满减门槛金额(类型1需要)" prop="couponFull">
          <el-input
            v-model="form.couponFull"
            placeholder="请输入满减门槛金额(类型1需要)"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item label="折扣率(0.9=9折)或直扣金额" prop="couponDeduction">
          <el-input
            v-model="form.couponDeduction"
            placeholder="请输入折扣率(0.9=9折)或直扣金额"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item label="最大折扣金额(类型2需要)" prop="couponMaxdeduction">
          <el-input
            v-model="form.couponMaxdeduction"
            placeholder="请输入最大折扣金额(类型2需要)"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item label="总库存" prop="couponTotal">
          <el-input
            v-model="form.couponTotal"
            placeholder="请输入总库存"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item label="剩余库存" prop="couponRemain">
          <el-input
            v-model="form.couponRemain"
            placeholder="请输入剩余库存"
            style="width: 300px"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="giveCoupon">
            直接发放优惠券
          </el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Couponmanage">
import {
  listCouponmanage,
  getCouponmanage,
  delCouponmanage,
  addCouponmanage,
  updateCouponmanage,
  givecoupon,
} from "@/api/coupon/couponmanage";

const { proxy } = getCurrentInstance();
const { coupon_type } = proxy.useDict("coupon_type");

const couponmanageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    couponType: null,
    couponFull: null,
    couponDeduction: null,
    couponMaxdeduction: null,
    couponTotal: null,
    couponRemain: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询优惠券列表 */
function getList() {
  loading.value = true;
  listCouponmanage(queryParams.value).then((response) => {
    console.log(response);
    couponmanageList.value = response.data;
    // couponmanageList.value = response.rows;
    // total.value = response.total;
    loading.value = false;
  });
}

/**
 * 直击发放优惠券
 */
function giveCoupon() {
  givecoupon(form.value).then((res) => {
    proxy.$modal.msgSuccess("发放成功");
    open.value = false;
    getList();
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    couponId: null,
    couponType: null,
    couponFull: null,
    couponDeduction: null,
    couponMaxdeduction: null,
    couponTotal: null,
    couponRemain: null,
  };
  proxy.resetForm("couponmanageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.couponId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加优惠券";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _couponId = row.couponId || ids.value;
  getCouponmanage(_couponId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改优惠券";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["couponmanageRef"].validate((valid) => {
    if (valid) {
      if (form.value.couponId != null) {
        debugger;
        updateCouponmanage(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCouponmanage(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _couponIds = row.couponId || ids.value;
  proxy.$modal
    .confirm('是否确认删除优惠券编号为"' + _couponIds + '"的数据项？')
    .then(function () {
      return delCouponmanage(_couponIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "coupon/couponmanage/export",
    {
      ...queryParams.value,
    },
    `couponmanage_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
